-
Notifications
You must be signed in to change notification settings - Fork 15
/
MessageReplyPreviewViewTests.swift
200 lines (157 loc) ยท 7.51 KB
/
MessageReplyPreviewViewTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// Wire
// Copyright (C) 2018 Wire Swiss GmbH
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//
import XCTest
@testable import Wire
import WireLinkPreview
extension UIView {
fileprivate func prepareForSnapshot(_ size: CGSize = CGSize(width: 320, height: 216)) -> UIView {
let container = ReplyRoundCornersView(containedView: self)
container.translatesAutoresizingMaskIntoConstraints = false
container.widthAnchor.constraint(equalToConstant: size.width).isActive = true
container.backgroundColor = SemanticColors.View.backgroundUserCell
return container
}
}
final class MessageReplyPreviewViewTests: ZMSnapshotTestCase {
override func setUp() {
super.setUp()
}
override func tearDown() {
invalidateStyle()
super.tearDown()
}
func invalidateStyle() {
NSAttributedString.invalidateMarkdownStyle()
NSAttributedString.invalidateParagraphStyle()
}
private func verifyInLightMode(message: MockMessage,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line) {
let sut = message.replyPreview()!.prepareForSnapshot()
verifyViewInLightScheme(createSut: {sut
}, file: file, testName: testName, line: line)
}
private func verifyInDarkMode(message: MockMessage,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line) {
let sut = message.replyPreview()!.prepareForSnapshot()
verifyViewInDarkScheme(createSut: { sut
}, file: file, testName: testName, line: line)
}
func testThatItRendersTextMessagePreview() {
let message = MockMessageFactory.textMessage(withText: "Lorem Ipsum Dolor Sit Amed.")
verifyInLightMode(message: message)
}
func testThatItRendersTextMessagePreview_DarkMode() {
let message = MockMessageFactory.textMessage(withText: "Lorem Ipsum Dolor Sit Amed.")
verifyInDarkMode(message: message)
}
func testThatItRendersEmojiOnly() {
let message = MockMessageFactory.textMessage(withText: "๐๐ฎ")
verifyInLightMode(message: message)
}
func testThatItRendersEmojiOnly_DarkMode() {
let message = MockMessageFactory.textMessage(withText: "๐๐ฎ")
verifyInDarkMode(message: message)
}
private func mentionMessage() -> MockMessage {
let message = MockMessageFactory.messageTemplate()
let textMessageData = MockTextMessageData()
textMessageData.messageText = "Hello @user"
let mockUser = SwiftMockLoader.mockUsers().first!
let mention = Mention(range: NSRange(location: 6, length: 5), user: mockUser)
textMessageData.mentions = [mention]
message.backingTextMessageData = textMessageData
return message
}
func testThatItRendersMention() {
verifyInLightMode(message: mentionMessage())
}
func testThatItRendersMention_DarkMode() {
verifyInDarkMode(message: mentionMessage())
}
func testThatItRendersTextMessagePreview_LongText() {
let message = MockMessageFactory.textMessage(withText: "Lorem Ipsum Dolor Sit Amed. Lorem Ipsum Dolor Sit Amed. Lorem Ipsum Dolor Sit Amed. Lorem Ipsum Dolor Sit Amed.")
verifyInLightMode(message: message)
}
func testThatItRendersTextMessagePreview_LongText_DarkMode() {
let message = MockMessageFactory.textMessage(withText: "Lorem Ipsum Dolor Sit Amed. Lorem Ipsum Dolor Sit Amed. Lorem Ipsum Dolor Sit Amed. Lorem Ipsum Dolor Sit Amed.")
verifyInDarkMode(message: message)
}
func testThatItRendersFileMessagePreview() {
let message = MockMessageFactory.fileTransferMessage()
verifyInLightMode(message: message)
}
func testThatItRendersFileMessagePreview_DarkMode() {
let message = MockMessageFactory.fileTransferMessage()
verifyInDarkMode(message: message)
}
func testThatItRendersLocationMessagePreview() {
let message = MockMessageFactory.locationMessage()
verifyInLightMode(message: message)
}
func testThatItRendersLocationMessagePreview_DarkMode() {
let message = MockMessageFactory.locationMessage()
verifyInDarkMode(message: message)
}
func testThatItRendersLinkPreviewMessagePreview() {
let url = "https://www.example.com/article/1"
let article = ArticleMetadata(originalURLString: url, permanentURLString: url, resolvedURLString: url, offset: 0)
article.title = "You won't believe what happened next!"
let message = MockMessageFactory.textMessage(withText: "https://www.example.com/article/1")
message.backingTextMessageData.backingLinkPreview = article
message.backingTextMessageData.linkPreviewImageCacheKey = "image-id-unsplash_matterhorn.jpg"
message.backingTextMessageData.imageData = image(inTestBundleNamed: "unsplash_matterhorn.jpg").jpegData(compressionQuality: 0.9)
message.backingTextMessageData.linkPreviewHasImage = true
let previewView = message.replyPreview()!
XCTAssertTrue(waitForGroupsToBeEmpty([MediaAssetCache.defaultImageCache.dispatchGroup]))
verify(matching: previewView.prepareForSnapshot())
}
func testThatItRendersImageMessagePreview() {
let image = self.image(inTestBundleNamed: "unsplash_matterhorn.jpg")
let message = MockMessageFactory.imageMessage(with: image)
let previewView = message.replyPreview()!
XCTAssert(waitForGroupsToBeEmpty([MediaAssetCache.defaultImageCache.dispatchGroup]))
verify(matching: previewView.prepareForSnapshot())
}
func disable_testThatItRendersVideoMessagePreview() {
let message = MockMessageFactory.fileTransferMessage()
message.backingFileMessageData.mimeType = "video/mp4"
message.backingFileMessageData.filename = "vacation.mp4"
message.backingFileMessageData.previewData = image(inTestBundleNamed: "unsplash_matterhorn.jpg").jpegData(compressionQuality: 0.9)
let previewView = message.replyPreview()!
XCTAssertTrue(waitForGroupsToBeEmpty([MediaAssetCache.defaultImageCache.dispatchGroup]))
verify(matching: previewView.prepareForSnapshot())
}
func testThatItRendersAudioMessagePreview() {
let message = MockMessageFactory.fileTransferMessage()
message.backingFileMessageData.mimeType = "audio/x-m4a"
message.backingFileMessageData.filename = "vacation.m4a"
let previewView = message.replyPreview()!
XCTAssertTrue(waitForGroupsToBeEmpty([MediaAssetCache.defaultImageCache.dispatchGroup]))
verify(matching: previewView.prepareForSnapshot())
}
func testDeallocation() {
let message = MockMessageFactory.textMessage(withText: "Lorem Ipsum Dolor Sit Amed.")
verifyDeallocation {
return message.replyPreview()!
}
}
}